package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	/**
	 * 查询1.2.3级分类信息
	 * @return
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//获取一二三级分类集合
		//创建一个list集合容器
		List<JSONObject>allList = new ArrayList<>();
		//创建一个view表 view表语法：create or replace 表名 as 多表内连接sql查询语句
		//view表可以看作一个表做crud 但底层还是通过查询各自的表获取数据
		//1.通过view表获取所有数据 获取所有数据 没有过滤条件 所以为null
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//2.获取所有数据后通过stream流 groupby的方式给数据分组
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		//分组后遍历map集合 创建容器要放在遍历集合里面 如果放在外面会把所有的二级分类都添加进来 不是只有当前一级分类添加进来
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
			Long category1Id = entry1.getKey();
			List<BaseCategoryView> category2List = entry1.getValue();
			String category1Name = category2List.get(0).getCategory1Name();
			List<JSONObject>jsonObjectList1 = new ArrayList<>();
			//根据遍历category1map获得的category2List 重复上述过程
			Map<Long, List<BaseCategoryView>> category3Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category3Map.entrySet()) {
				Long category2Id = entry2.getKey();
				List<BaseCategoryView> category3List = entry2.getValue();
				String category2Name = category3List.get(0).getCategory2Name();


				List<JSONObject>jsonObjectList2 = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : category3List) {
					Long category3Id = baseCategoryView.getCategory3Id();
					String category3Name = baseCategoryView.getCategory3Name();
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId",category3Id);
					jsonObject3.put("categoryName",category3Name);
					jsonObjectList2.add(jsonObject3);
				}
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId",category2Id);
				jsonObject2.put("categoryName",category2Name);
				jsonObject2.put("categoryChild",jsonObjectList2);
				jsonObjectList1.add(jsonObject2);

			}


			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId",category1Id);
			jsonObject1.put("categoryName",category1Name);
			jsonObject1.put("categoryChild",jsonObjectList1);
			allList.add(jsonObject1);


		}

		return allList;

	}

	/**
	 * 根据一级分类查询所有的属性列表供选择
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		List<BaseAttribute>baseAttributeList = baseAttributeMapper.selectAttributesByCategory1Id(category1Id);

		return baseAttributeList;

	}
}
